Ajax.Responders.register({
    onCreate: function(){
      Effect.Appear('actions',{duration:1});
    }, 
    onComplete: function(){
       Effect.Fade('actions', {duration:0.5});
    }
  });

/* Start Shadowbox */
Shadowbox.init({
    skipSetup: true,
    players: ["html"]
});

function set_value($id,$value){
  $($id).value = $value;
}
function open_icons_box(){
    // open a welcome message as soon as the window loads
    Shadowbox.open({
        content:    $('icons_box').innerHTML,
        player:     "html",
        title:      "Icons",
        height:     350,
        width:      350
    });
}
function select_icon($icon,$src){
  $('icon_thumb').src = $src;
  $('icon_name').innerHTML = $icon;
  $('icon').value = $icon;
  Shadowbox.close();
}

function toggle_box($image,$content){
	var $src = $($image).src;
	var $last_src = $src.substr($src.lastIndexOf('/'),255);
	if($last_src == '/icon-down.gif'){
		$new_src = 'modules/admin/media/icon-up.gif';
	} else {
		$new_src = 'modules/admin/media/icon-down.gif';
	}
	$($image).src = $new_src;
	new Effect.toggle($content,'slide');
}



function toggle_big_box($image,$content){
	var $src = $($image).src;
	var $last_src = $src.substr($src.lastIndexOf('/'),255);
	if($last_src == '/despl.gif'){
		$new_src = 'modules/admin/media/despl-up.gif';
	} else {
		$new_src = 'modules/admin/media/despl.gif';
	}
	$($image).src = $new_src;
	new Effect.toggle($content,'blind');
}



function clean_filed($fieldname){
  $($fieldname).value = '';
}

function processtag(elm){
    $('tags').value = '';
    $tag = elm.innerHTML;
    $string = $('used_tags').value;
    var $array = $string.split(",");
    $repeat = false;
    for (var $i = 0; $i < $array.length-1; $i++){
	if($tag == $array[$i]){
	  $repeat = true;
	}
    }
    if($repeat == false){
      $('used_tags').value = $('used_tags').value+$tag+',';
      $('tags_holder').innerHTML = $('tags_holder').innerHTML+'<span id="tag'+$tag+'" class="tag_holded"><a onclick="delete_tag(\''+$tag+'\');">'+$tag+'</a></span> ';
    }
}
function add_tag($tag){
    $('tags').value = '';
    $string = $('used_tags').value;
    var $array = $string.split(",");
    $repeat = false;
    for (var $i = 0; $i < $array.length-1; $i++){
	if($tag == $array[$i]){
	  $repeat = true;
	}
    }
    if($repeat == false){
      $('used_tags').value = $('used_tags').value+$tag+',';
      $('tags_holder').innerHTML = $('tags_holder').innerHTML+'<span onclick="delete_tag(\''+$tag+'\');" id="tag'+$tag+'" class="tag_holded">'+$tag+'</span> ';
    }
}
function delete_tag($tag){
  $('tag'+$tag).fade( { queue: 'start' } );
  Effect.Queues.get('tag'+$tag).interval = 1000;
  $ctag = $tag+',';
  $used = $('used_tags').value;
  var $rtag = new RegExp ($ctag, 'gi') ; 
  $new = $used.replace($rtag, "");
  $('used_tags').value = $new;
  setTimeout("$('tag'+$tag).remove()",2500);
}



function processtopic(elm){
    $('topics').value = '';
    $topic = elm.innerHTML;
    $string = $('used_topics').value;
    var $array = $string.split(",");
    $repeat = false;
    for (var $i = 0; $i < $array.length-1; $i++){
	if($topic == $array[$i]){
	  $repeat = true;
	}
    }
    if($repeat == false){
      $('used_topics').value = $('used_topics').value+$topic+',';
      $('topics_holder').innerHTML = $('topics_holder').innerHTML+'<span id="topic'+$topic+'" class="topic_holded"><a onclick="delete_topic(\''+$topic+'\');">'+$topic+'</a></span> ';
    }
}
function add_topic($topic){
    $('topics').value = '';
    $string = $('used_topics').value;
    var $array = $string.split(",");
    $repeat = false;
    for (var $i = 0; $i < $array.length-1; $i++){
	if($topic == $array[$i]){
	  $repeat = true;
	}
    }
    if($repeat == false){
      $('used_topics').value = $('used_topics').value+$topic+',';
      $('topics_holder').innerHTML = $('topics_holder').innerHTML+'<span onclick="delete_topic(\''+$topic+'\');" id="topic'+$topic+'" class="topic_holded">'+$topic+'</span> ';
    }
}
function delete_topic($topic){
  $('topic'+$topic).fade( { queue: 'start' } );
  Effect.Queues.get('topic'+$topic).interval = 1000;
  $ctopic = $topic+',';
  $used = $('used_topics').value;
  var $rtopic = new RegExp ($ctopic, 'gi') ; 
  $new = $used.replace($rtopic, "");
  $('used_topics').value = $new;
  setTimeout("$('topics'+$topic).remove()",2500);
}



function current_vertical_item($a,$caption,$load_caption,$total){
  $use = $caption+$a;
  for($i=1;$i<=$total;$i++){
    if($caption+$i == $use){
      $($caption+$i).addClassName('vertical-list-current');
      $($load_caption+$i).toggle();
      new Effect.Pulsate($caption+$i,{pulses:1, duration:1.5});
    } else {
      $($caption+$i).removeClassName('vertical-list-current');
      $($load_caption+$i).hide();
    }
  }
  new Effect.ScrollTo('box-content');
}
function toggle_sex($id){
  if($id == 1){
      $('sex2').className = 'publish';
      $('sex1').className = 'save';
      $('sex1').setOpacity(1);
      $('sex2').setOpacity(0.5);
      $('sex').value = 1;
  } else {
      $('sex1').className = 'publish';
      $('sex2').className = 'save';
      $('sex1').setOpacity(0.5);
      $('sex2').setOpacity(1);
      $('sex').value = 2;
  }
}


function add_custom_field(){

    $name = $('custom_field_name').value;
    $value = $('custom_field_value').value;
    $string = $('list_custom_field_names').value;

    var $array = $string.split(",");
    $repeat = false;
    for (var $i = 0; $i < $array.length-1; $i++){
	if($name == $array[$i]){
	  $repeat = true;
	}
    }

    if($repeat == false && $name.length > 3 && $value.length > 3){
      $('list_custom_field_names').value = $('list_custom_field_names').value+$name+',';
      $('list_custom_field_values').value = $('list_custom_field_values').value+$value+',';
      $('custom_field_names').innerHTML = $('custom_field_names').innerHTML+'<img id="delimg'+$name+'" onclick="delete_custom_field($name,$value);" src="modules/admin/media/icons/8x8/close.png" class="delete_custom_field"/><div id="custom_field_name'+$name+'">'+$name+'</div><br/>';
      $('custom_field_values').innerHTML = $('custom_field_values').innerHTML+'<div id="custom_field_value'+$value+'">'+$value+'</div><br/>';

      var $array = $('list_custom_field_names').value.split(",");
      for (var $i = 0; $i < $array.length-1; $i++){
	 $names = $array[$i];
	 new Ajax.InPlaceEditor('custom_field_name'+$array[$i], 'modules/admin/controllers/inc/inplaceeditor.php', { callback: function(form, value) { return 'value='+value+'&type=names&all_values='+escape($('list_custom_field_names').value) } });
      }

      var $array = $('list_custom_field_values').value.split(",");
      for (var $i = 0; $i < $array.length-1; $i++){
	 $values = $array[$i];
	 new Ajax.InPlaceEditor('custom_field_value'+$array[$i], 'modules/admin/controllers/inc/inplaceeditor.php',{ callback: function(form, value) { return 'value='+value+'&type=values&all_values='+escape($('list_custom_field_values').value) } });
      }
      
      
      $('custom_field_name').value = '';
      $('custom_field_value').value = '';
    }
}

function delete_custom_field($name,$value){
  $('custom_field_name'+$name).fade();
  $('custom_field_value'+$value).fade();
  $('delimg'+$name).fade();

  $cname = $name+',';
  $used = $('list_custom_field_names').value;
  var $rname = new RegExp ($cname, 'gi') ; 
  $new = $used.replace($rname, "");
  $('list_custom_field_names').value = $new;
  setTimeout("$('custom_field_name'+$name).remove()",2500);

  $cvalue = $value+',';
  $used = $('list_custom_field_values').value;
  var $rvalue = new RegExp ($cvalue, 'gi') ; 
  $new = $used.replace($rvalue, "");
  $('list_custom_field_values').value = $new;
  setTimeout("$('custom_field_value'+$value).remove()",2500);
  
}

function submit_post_form($status){
  $('status').value = $status;
  $('new_post').submit();
}
function submit_page_form($status){
  $('status').value = $status;
  $('new_page').submit();
}
function submit_form($status,$form){
  $('status').value = $status;
  $($form).submit();
}

var oldLink = null;
// code to change the active stylesheet
function setActiveStyleSheet(link, title) {
  var i, a, main;
  for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
    if(a.getAttribute("rel").indexOf("style") != -1 && a.getAttribute("title")) {
      a.disabled = true;
      if(a.getAttribute("title") == title) a.disabled = false;
    }
  }
  if (oldLink) oldLink.style.fontWeight = 'normal';
  oldLink = link;
  link.style.fontWeight = 'bold';
  return false;
}

// This function gets called when the end-user clicks on some date.
function selected(cal, date) {
  cal.sel.value = date; // just update the date in the input field.
  if (cal.dateClicked && (cal.sel.id == "sel1" || cal.sel.id == "sel3"))
    // if we add this call we close the calendar on single-click.
    // just to exemplify both cases, we are using this only for the 1st
    // and the 3rd field, while 2nd and 4th will still require double-click.
    cal.callCloseHandler();
}

// And this gets called when the end-user clicks on the _selected_ date,
// or clicks on the "Close" button.  It just hides the calendar without
// destroying it.
function closeHandler(cal) {
  cal.hide();                        // hide the calendar
//  cal.destroy();
  _dynarch_popupCalendar = null;
}

// This function shows the calendar under the element having the given id.
// It takes care of catching "mousedown" signals on document and hiding the
// calendar if the click was outside.
function showCalendar(id, format, showsTime, showsOtherMonths) {
  var el = document.getElementById(id);
  if (_dynarch_popupCalendar != null) {
    // we already have some calendar created
    _dynarch_popupCalendar.hide();                 // so we hide it first.
  } else {
    // first-time call, create the calendar.
    var cal = new Calendar(1, null, selected, closeHandler);
    // uncomment the following line to hide the week numbers
    // cal.weekNumbers = false;
    if (typeof showsTime == "string") {
      cal.showsTime = true;
      cal.time24 = (showsTime == "24");
    }
    if (showsOtherMonths) {
      cal.showsOtherMonths = true;
    }
    _dynarch_popupCalendar = cal;                  // remember it in the global var
    cal.setRange(1900, 2070);        // min/max year allowed.
    cal.create();
  }
  _dynarch_popupCalendar.setDateFormat(format);    // set the specified date format
  _dynarch_popupCalendar.parseDate(el.value);      // try to parse the text in field
  _dynarch_popupCalendar.sel = el;                 // inform it what input field we use

  // the reference element that we pass to showAtElement is the button that
  // triggers the calendar.  In this example we align the calendar bottom-right
  // to the button.
  _dynarch_popupCalendar.showAtElement(el.nextSibling, "Br");        // show the calendar

  return false;
}

var MINUTE = 60 * 1000;
var HOUR = 60 * MINUTE;
var DAY = 24 * HOUR;
var WEEK = 7 * DAY;

// If this handler returns true then the "date" given as
// parameter will be disabled.  In this example we enable
// only days within a range of 10 days from the current
// date.
// You can use the functions date.getFullYear() -- returns the year
// as 4 digit number, date.getMonth() -- returns the month as 0..11,
// and date.getDate() -- returns the date of the month as 1..31, to
// make heavy calculations here.  However, beware that this function
// should be very fast, as it is called for each day in a month when
// the calendar is (re)constructed.
function isDisabled(date) {
  var today = new Date();
  return (Math.abs(date.getTime() - today.getTime()) / DAY) > 10;
}

function flatSelected(cal, date) {
  var el = document.getElementById("preview");
  el.innerHTML = date;
}

function showFlatCalendar() {
  var parent = document.getElementById("display");

  // construct a calendar giving only the "selected" handler.
  var cal = new Calendar(0, null, flatSelected);

  // hide week numbers
  cal.weekNumbers = false;

  // We want some dates to be disabled; see function isDisabled above
  cal.setDisabledHandler(isDisabled);
  cal.setDateFormat("%A, %B %e");

  // this call must be the last as it might use data initialized above; if
  // we specify a parent, as opposite to the "showCalendar" function above,
  // then we create a flat calendar -- not popup.  Hidden, though, but...
  cal.create(parent);

  // ... we can show it here.
  cal.show();
}
function load_editor($id){
  	tinyMCE.init({
		// General options
		mode : "exact",
		elements : $id,
		theme : "advanced",
		skin : "o2k7",
		skin_variant : "silver",
		plugins : "tinyBrowser,safari,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template,inlinepopups",
		file_browser_callback : 'tinyBrowser',
		// Theme options
		theme_advanced_buttons1 : "save,newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect",
		theme_advanced_buttons2 : "cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor",
		theme_advanced_buttons3 : "tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen",
		theme_advanced_buttons4 : "insertlayer,moveforward,movebackward,absolute,|,styleprops,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,pagebreak",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		theme_advanced_resizing : true,

		// Example word content CSS (should be your site CSS) this one removes paragraph margins
		content_css : "css/word.css",

		// Drop lists for link/image/media/template dialogs
		template_external_list_url : "lists/template_list.js",
		external_link_list_url : "lists/link_list.js",
		external_image_list_url : "lists/image_list.js",
		media_external_list_url : "lists/media_list.js"
	});

}
function IsChecked(el) {
    return $$('#' + el + ' input').find(
        function(e) {return e.checked;});  
}
function AddCheckbox(item){
	if($(item).checked == true){
		$('checkboxes').value = $('checkboxes').value+item+',';
	} else {
		RemoveCheckbox(item+',');
	}
}
function RemoveCheckbox($id){
  $used = $('checkboxes').value;
  var $rep = new RegExp ($id, 'gi') ; 
  $new = $used.replace($rep, "");
  $('checkboxes').value = $new;
  GetBulkIds();
}
function GetBulkIds(){
  $used = $('checkboxes').value;
  var $rep = new RegExp ('entry_', 'gi') ; 
  $new = $used.replace($rep, "");
  return $new;
}
function BulkActions_Blog_Delete(){
	$ids = GetBulkIds();
	location.href='index.php?module=admin&file=content.tpl&delid='+$ids;
}
function BulkActions_Feedback($action){
	$ids = GetBulkIds();
	location.href='index.php?module=admin&file=feedback.tpl&comment_action='+$action+'&id='+$ids;
}
